package com.dongdongshop.controller;

import com.dongdongshop.util.Result;
import com.dongdongshop.enums.ResultEnum;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.service.ItemCatService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("cat")
public class ItemCatController {

    @DubboReference
    private ItemCatService is;


    @RequestMapping("show")
    public String show(){
        return "admin/item_cat";
    }

    @RequestMapping("getSelect")
    @ResponseBody
    public Result getSelect(Long parentId){
        try {
            List<TbItemCat> select = is.getSelect(parentId);
            return Result.OK().setData(select);
        } catch (Exception e) {
            return Result.response(ResultEnum.ERROR);
        }
    }

    @RequestMapping("getId")
    @ResponseBody
    public Result getId(Long id){
        try {
            TbItemCat id1 = is.getId(id);
            return Result.OK().setData(id1);
        } catch (Exception e) {
            return Result.response(ResultEnum.SELECT_MESSAGE_ERROE);
        }
    }

    @RequestMapping("insert")
    @ResponseBody
    public Result insert(TbItemCat c){
        try {
            if (c.getParentId() == null) {
                c.setParentId(Long.valueOf(0));
            }
            is.insert(c);
            return Result.OK();
        } catch (Exception e) {
            return Result.response(ResultEnum.ADD_MESSAGE_ERROE);
        }
    }

    @RequestMapping("getById")
    @ResponseBody
    public Result getById(Long id){
        try {
            TbItemCat byId = is.getById(id);
            return Result.OK().setData(byId);
        } catch (Exception e) {
            return Result.response(ResultEnum.GETBYID_ERROR);
        }
    }

    @RequestMapping("updateId")
    @ResponseBody
    public Result updateId(Long id){
        try {
            TbItemCat tbItemCat = is.updateId(id);
            return Result.OK().setData(tbItemCat);
        } catch (Exception e) {
            return Result.response(ResultEnum.GETBYID_ERROR);
        }
    }

    @RequestMapping("update")
    @ResponseBody
    public Result update(TbItemCat c){
        try {
            is.update(c);
            return Result.OK();
        } catch (Exception e) {
            return Result.response(ResultEnum.UPDATE_MESSAGE_ERROE);
        }
    }

    @RequestMapping("deleteCheck")
    @ResponseBody
    public Result deleteCheck(Long[] ids){
        try {
            is.deleteCheck(ids);
            return Result.OK();
        } catch (Exception e) {
            return Result.response(ResultEnum.DELETE_MESSAGE_ERROE);
        }
    }
}
